﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Top.Api;
using Top.Api.Domain;
using Top.Api.Request;
using Top.Api.Response;
using DBControl;

namespace SellerProject
{
    public class Marketing
    {
        private string skey;
        private static ITopClient itopclient = new DefaultTopClient(SystemLib.appurl, "12268933", "5c8fc265d41463b42f222d91f9f55fdf");

        private UserTag mytag = null;
        /// <summary>
        /// 只读：标签人群
        /// </summary>
        public UserTag myTag { get { return mytag; } private set { mytag = value; }}


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sessionkey"></param>
        public Marketing(string sessionkey)
        {
            this.skey = sessionkey;
        }
        /// <summary>
        /// 添加人群标签
        /// </summary>
        /// <param name="tagname">标签名</param>
        /// <param name="tagdescription">标签描述</param>
        public void Tag_add(string tagname, string tagdescription)
        {
            MarketingTagAddRequest req = new MarketingTagAddRequest();
            req.TagName = tagname;
            req.Description = tagdescription;
            MarketingTagAddResponse response = itopclient.Execute(req, skey);
        }
        /// <summary>
        /// 按标签id查询人群
        /// </summary>
        /// <param name="tagname">要查询的tagid</param>
        public UserTag Tag_get(long tagid)
        {
            MarketingTagsGetRequest req = new MarketingTagsGetRequest();
            req.Fields = "tag_id,tag_name";
            MarketingTagsGetResponse response = itopclient.Execute(req, skey);
            mytag = response.UserTags.Find(delegate(UserTag ut) { return ut.TagId == tagid; });
            return mytag;
            //response.UserTags.Where(f => (f as UserTag).TagName == tagid).First();

            //Predicate<UserTag> findtag = delegate(UserTag ut) { return ut.TagName == tagid; };

            //List<string> slist = new List<string>();
            //slist.Where(f => f == "abc").First();
        }
        /// <summary>
        /// 按标签名查询人群
        /// </summary>
        /// <param name="tagname">要查询的标签名</param>
        /// <returns>UserTag</returns>
        public UserTag Tag_get(string tagname)
        {
            MarketingTagsGetRequest req = new MarketingTagsGetRequest();
            req.Fields = "tag_id,tag_name";
            MarketingTagsGetResponse response = itopclient.Execute(req, skey);
            mytag = response.UserTags.Find(delegate(UserTag ut) { return ut.TagName == tagname; });
            return mytag;
        }
        /// <summary>
        /// 向人群中添加用户
        /// </summary>
        /// <param name="tagid">添加到的目标人群id</param>
        /// <param name="usnick">要添加的用户昵称</param>
        public void TargetUser_add(long tagid, string usnick)
        {
            MarketingTaguserAddRequest req = new MarketingTaguserAddRequest();
            req.TagId = tagid;
            req.Nick = usnick;
            MarketingTaguserAddResponse response = itopclient.Execute(req, skey);
        }
        /// <summary>
        /// 添加定向优惠策略
        /// </summary>
        /// <param name="numid">要打折产品数字ID</param>
        /// <param name="tagid">定向人群ID</param>
        /// <param name="disconttype">打折类型，分为DISCOUNT折扣和PRICE减价</param>
        /// <param name="discontvalue">打折的值，如是DISCOUNT则填入0.1~9.9之间的数字，如是PRICE则直接填入价格</param>
        /// <param name="endtime">结束时间</param>
        public void Promotion_add(string numid, long tagid,string disconttype,string discontvalue,DateTime endtime)
        {
            MarketingPromotionAddRequest req = new MarketingPromotionAddRequest();
            req.NumIids = numid;
            req.DiscountType = disconttype;
            req.DiscountValue = discontvalue;
            DateTime dateTime = DateTime.Parse(DateTime.Now.ToString("yyyy-mm-dd HH:mm:ss"));
            req.StartDate = dateTime;
            DateTime dateTime1 = DateTime.Parse(endtime.ToString("yyyy-mm-dd HH:mm:ss"));
            req.EndDate = dateTime1;
            req.TagId = tagid;
            MarketingPromotionAddResponse response = itopclient.Execute(req, skey);
        }

        /// <summary>
        /// 查询定向优惠策略
        /// </summary>
        /// <param name="tagid">定向人群ID</param>
        /// <param name="numid">定向商品ID</param>
        /// <returns>MarketingPromotionsgetResponse</returns>
        public MarketingPromotionsGetResponse Promotion_get( long tagid, string numid )
        {
            MarketingPromotionsGetRequest req = new MarketingPromotionsGetRequest();
            //req.Status = "ACTIVE";
            req.Fields = "promotion_id, promotion_title, item_id,start_date,end_date,discount_value,status";
            req.TagId = tagid;
            req.NumIid = numid;
            MarketingPromotionsGetResponse response = itopclient.Execute(req, skey);
            return response;
        }
    }
}